package com.springcloud.sc.ucpv5.client.util;

import com.sun.corba.se.impl.encoding.CDROutputStream;

import java.util.Properties;

public class CorbarUtil {

	// Corba的orb的引用
	private static org.omg.CORBA.ORB orbRef = null;

	static {
		Properties properties = System.getProperties();
		if (properties.get("org.omg.CORBA.ORBClass") == null) {
			properties.put("org.omg.CORBA.ORBClass", "org.jacorb.orb.ORB");
			properties.put("org.omg.CORBA.ORBSingletonClass", "org.jacorb.orb.ORBSingleton");
		}
		orbRef = org.omg.CORBA.ORB.init((String[]) null, properties);
	}

	/**
	 * 调用corba的方法把结构转换成字节流，前台向后台发送命令时要调用此方法把结构转换成字节流。
	 * 
	 * @param struct
	 *            由idl生成的以Holder结尾的类;
	 * @return 0表示成功，
	 */
	public static byte[] struct2Byte(org.omg.CORBA.portable.Streamable struct) {

		if (struct == null) {
			return null;
		}

		/*try {



			org.jacorb.orb.CDRUtf8OutputStream stream = new org.jacorb.orb.CDRUtf8OutputStream(orbRef);
			stream.write_short((short) 0);
			struct._write(stream);
			return stream.getBufferCopy();
		} catch (Exception e) {
			return null;
		}*/
		return null;
	}

	/**
	 * 调用corba的方法把字节流转换成结构，前台向后台发送命令后的返回值要调用此方法把字节流转换成结构。
	 * 
	 * @param data
	 *            需要转换的字节流;
	 * @param struct
	 *            由idl生成的以Holder结尾的类;
	 * @return 0表示成功，其它表示转换失败;
	 */
	public static int byte2Struct(byte[] data, org.omg.CORBA.portable.Streamable struct) {
		if (data == null || struct == null || data.length < 1) {
			return 0;
		}
		/*try {
			org.jacorb.orb.CDRUtf8InputStream stream = new org.jacorb.orb.CDRUtf8InputStream(orbRef, data, true);
			short byte_order = stream.read_short();
			if (byte_order == 0) {
				stream.setLittleEndian(false);
			} else {
				stream.setLittleEndian(true);
			}
			struct._read(stream);
		} catch (Exception e) {
			// 设置当前版本类型
			// PublicUtil.printCorbaObject(msg+" 解码异常",null);
			return 6033;
		}*/

		return 0;

	}
}
